/**
 * PointPath
 *
 */
if(typeof (module) !="undefined"){
    module.exports = PointPath;

    var Point = require('./Point');

}

// 简单路径
function PointPath() {
    // 包含多个点
    this.points = new Array();// 二维数组，每个点是一个数组
    this.w = 0;
    this.h = 0;
}
// 转成json字符串
// 将多个点用数组表示，不需要按对象转，可以节省空间
// 按以下方式储存可以节省75%的空间:
//		{	"points":[	[0,0,1],
//			[1,1,1],
//			[2,2,1]],
//			"w":100,
//			"h":200}

PointPath.prototype.toJson = function () {
    var ret = '{';
    var pts = '[';
    if (this.points.length > 0) {
        for (var i = 0; i < this.points.length; i++) {
            pts += this.points[i].toArray();
            pts += ','
        }
        pts = pts.substr(0, pts.length - 1);// 去除多余的逗号
    }
    pts += ']';

    ret += ('"points":' + pts);
    ret += ',';
    ret += ('"w":' + this.w);
    ret += ',';
    ret += ('"h":' + this.h);
    ret += '}';
    return ret;
}
// 从json对象中解析出PointPath
PointPath.prototype.fromJson = function (jsonObj, versionId) {
    var pts = jsonObj.points;
    this.w = jsonObj.w*1;
    this.h = jsonObj.h*1;
    this.points.splice(0, this.points.length);
    for (var i = 0; i < pts.length; i++) {
        var newPt = new Point();
        newPt.fromArray(pts[i]);
        this.points.push(newPt);
    }

}
/**
 * 利用ParaControu的矩阵和CompoundPath的矩阵更新轮廓点
 * 参数：
 *      pcMatrix：ParaContour的矩阵，可能为null
 */
PointPath.prototype.multiplyMatrix = function(pcMatrix){
    if(!pcMatrix)	return;
    
    for(var l = 0; l < this.points.length; l++) {
        var pt = this.points[l] ;
        pt.multiplyMatrix(pcMatrix);
    }
}
/**
 * 参数：
 *      offsetX，offsetY （可选参数） 偏移量
 * 转换成SVG格式的字符串：
 *      'M 0,20 L 100,160 Q 130,200 150,120 C 190,-40 200,200 300,150 L 400,90 z',
 * 其中：
 *      M   MoveTo
 *      L   LineTo
 *      Q   Quadratic
 *      C   Cubic
 *      z   closepath
 */
PointPath.prototype.toSvgPath = function (offsetX, offsetY) {
    offsetX = (offsetX == null) ? 0 : offsetX;
    offsetY = (offsetY == null) ? 0 : offsetY;

    var svg = "";
    var closeMark=false;
    for (var i = 0; i < this.points.length; i++) {
        var pt = this.points[i];

        switch (pt.type) {
            case Point.prototype.POD_TT_PRIM_MOVE:
                svg += ("M " + (pt.x + offsetX) + ',' + (pt.y + offsetY) + ' ');
                break;
            case Point.prototype.POD_TT_PRIM_LINE:
                svg += ("L " + (pt.x + offsetX) + ',' + (pt.y + offsetY) + ' ');
                break;
            case Point.prototype.POD_TT_PRIM_QSPLINE:
                svg += ("Q " + (pt.x + offsetX) + ',' + (pt.y + offsetY) + ' ');
                var ptN = this.points[i + 1];
                svg += ( (ptN.x + offsetX) + ',' + (ptN.y + offsetY) + ' ');
                i++;
                break;
            case Point.prototype.POD_TT_PRIM_CSPLINE:
                svg += ("C " + (pt.x + offsetX) + ',' + (pt.y + offsetY) + ' ');
                var ptN = this.points[i + 1];
                svg += ((ptN.x + offsetX) + ',' + (ptN.y + offsetY) + ' ');
                var ptNN = this.points[i + 2];
                svg += ((ptNN.x + offsetX) + ',' + (ptNN.y + offsetY) + ' ');
                i += 2;
                break;
            case Point.prototype.POD_TT_PRIM_CLOSE:
                svg += ("z " );
                closeMark = true;
                break;
            default :
                break;
        }

    }
    // 检测是否为闭合路径
    if (!closeMark && (this.points.length > 2) ) {
        var tolerance = 0.05;
        if (Math.abs(this.points[0].x - this.points[this.points.length - 1].x) < tolerance
            && Math.abs(this.points[0].y - this.points[this.points.length - 1].y) < tolerance)
            svg += 'z';
    }
    return svg;
}

// 创建表格格子
// 参数
//		rtX：格子的起点
//		rtY：格子的起点
//		rtW：格子的宽度
//		rtH：格子的宽度
PointPath.prototype.generateRect = function (rtX, rtY, rtW, rtH) {
    var pp = new PointPath();
    pp.w = rtW;
    pp.h = rtH;

    var pt = new Point();
    pt.type = Point.prototype.POD_TT_PRIM_MOVE;
    pt.x = rtX;
    pt.y = rtY;
    pp.points.push(pt);

    pt = new Point();
    pt.type = 1;
    pt.x = rtX;
    pt.y = rtY + rtH;
    pp.points.push(pt);

    pt = new Point();
    pt.type = 1;
    pt.x = rtX + rtW;
    pt.y = rtY + rtH;
    pp.points.push(pt);

    pt = new Point();
    pt.type = 1;
    pt.x = rtX + rtW;
    pt.y = rtY;
    pp.points.push(pt);

    pt = new Point();
    pt.type = Point.prototype.POD_TT_PRIM_CLOSE;
    pt.x = rtX;
    pt.y = rtY;
    pp.points.push(pt);

    return pp;
}
// 调整简单路径，平移或者缩放
// 参数
//		charContour:复合路径
//		vW:水平方向的平移值或者缩放值
//		vH:垂直方向的平移值或者缩放值
// 		type:类型，1 平移  2 缩放
PointPath.prototype.adjustPosition = function (vW, vH, type) {
    if (this.points.length == 0)
        return;
    if (2 == type) {
        this.w *= vW;
        this.h *= vH;
    }

    for (var l = 0; l < this.points.length; l++) {
        var pt = this.points[l];
        if (type == 1) {
            pt.x += vW;
            pt.y += vH;
        } else if (type == 2) {
            pt.x *= vW;
            pt.y *= vH;
        }
    }
    return;
}
